package com.kfm.servlet;

import com.kfm.mapper.IClassesMapper;
import com.kfm.mapper.IStudentMapper;
import com.kfm.service.ClassesService;
import com.kfm.service.StudentService;
import com.kfm.util.Contains;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/studelete")
public class DeleteStudentServlet extends HttpServlet {

    private StudentService service;

    @Override
    public void init() throws ServletException {
        ServletContext servletContext = getServletContext();
        Object attribute = servletContext.getAttribute(Contains.SQL_SESSION_FACTORY);

        if (attribute instanceof SqlSessionFactory sqlSessionFactory){

            SqlSession sqlSession = sqlSessionFactory.openSession(true);
            IStudentMapper studentMapper = sqlSession.getMapper(IStudentMapper.class);
            service = new StudentService(studentMapper);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");

        int delete = service.delete(id);
        resp.sendRedirect("/student");

    }
}
